#!/bin/env python
#-*- coding:utf-8 -*-

#----------------------------------------------------------------------------
# Name     :      GUI_grid.py
# Purpose  :      gui's custom grid code module
# Author   :      Albert cheng
# Created  :      2012-10-29
# Copyright:     (c) 2012 by Albertcheng
# Licence  :      wxWindows license
#----------------------------------------------------------------------------

import  wx.grid as gridlib

GRID_STRING = gridlib.GRID_VALUE_STRING
GRID_NUMBER = gridlib.GRID_VALUE_NUMBER
GRID_BOOL   = gridlib.GRID_VALUE_BOOL
GRID_CHOICE = gridlib.GRID_VALUE_CHOICE



class CustomDataTable(gridlib.PyGridTableBase):
    def __init__(self,Labels,dt,maxrow,maxcol,data):
        gridlib.PyGridTableBase.__init__(self)
        #self.log = log
        #Labels = ['Prop表ID', '道具名', '道具数量']
        self.colLabels = Labels
        self.dataTypes = dt
        
        self.data = data
        self.maxrow = maxrow
        self.maxcol = maxcol
        
        
    #--------------------------------------------------
    # required methods for the wxPyGridTableBase interface

    def GetNumberRows(self):
        dl = len(self.data) + 1
        if dl < self.maxrow:
            dl = self.maxrow
        return dl

    def GetNumberCols(self):
        if self.data != []:
            return len(self.data[0])
        else:
            return self.maxcol
    def IsEmptyCell(self, row, col):
        try:
            return not self.data[row][col]
        except IndexError:
            return True

    # Get/Set values in the table.  The Python version of these
    # methods can handle any data-type, (as long as the Editor and
    # Renderer understands the type too,) not just strings as in the
    # C++ version.
    def GetValue(self, row, col):
        try:
            return self.data[row][col]
        except IndexError:
            return ''

    def SetValue(self, row, col, value):
        def innerSetValue(row, col, value):
            try:
                self.data[row][col] = value
            except IndexError:
                # add a new row
                self.data.append([''] * self.GetNumberCols())
                innerSetValue(row, col, value)

        innerSetValue(row, col, value) 
        #self.ResetView(self)
    #--------------------------------------------------
    # Some optional methods

    # Called when the grid needs to display labels
    def GetColLabelValue(self, col):
        return self.colLabels[col]

    # Called to determine the kind of editor/renderer to use by
    # default, doesn't necessarily have to be the same type used
    # natively by the editor/renderer if they know how to convert.
    def GetTypeName(self, row, col):
        return self.dataTypes[col]

    # Called to determine how the data can be fetched and stored by the
    # editor and renderer.  This allows you to enforce some type-safety
    # in the grid.
    def CanGetValueAs(self, row, col, typeName):
        colType = self.dataTypes[col].split(':')[0]
        if typeName == colType:
            return True
        else:
            return False

    def CanSetValueAs(self, row, col, typeName):
        return self.CanGetValueAs(row, col, typeName)

class CustTableGrid(gridlib.Grid):
    def __init__(self, parent, Tags, dt, colsize, data):
        gridlib.Grid.__init__(self, parent, -1)
        
        
        maxrow = 100
        self.table = CustomDataTable(Tags,dt,maxrow,len(Tags),data)
        #print dir(self.table)
        # The second parameter means that the grid is to take ownership of the
        # table and will destroy it when done.  Otherwise you would need to keep
        # a reference to it and call it's Destroy method later.
        self.SetTable(self.table, True)
        
        self.SetRowLabelSize(25)
        
        for n in colsize:
            #print n
            self.SetColSize(n[0],n[1])

        gridlib.EVT_GRID_CELL_LEFT_DCLICK(self, self.OnLeftDClick)
        gridlib.EVT_GRID_CELL_CHANGE(self, self.OnChange)
    
    def OnChange(self,evt):
        row = evt.GetRow()
        col = evt.GetCol()
        if col == 2:
            self.SetValue(row,0,1)
            
        
        
        
        
    def SetValue(self,row,col,v):
        self.table.SetValue(row,col,v)
        #self.SetTable(self.table, True)
        self.ForceRefresh()
    # I do this because I don't like the default behaviour of not starting the
    # cell editor on double clicks, but only a second click.
    def OnLeftDClick(self, evt):
        if self.CanEnableCellControl():
            self.EnableCellEditControl()
    def GetValue(self,row,col):
        return self.table.GetValue(row,col)
